package com.bawei.module_home.ui.devices

import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_common.utils.TimeUtils
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityInsertDeviceBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.DeviceModel
import com.bumptech.glide.Glide
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.INSERT_DEVICE_PAGE)
class InsertDeviceActivity : BaseActivity<BaseViewModel, ActivityInsertDeviceBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_insert_device

    private val mViewModel: HomeServiceViewModel by viewModels()
    override fun initView() {
        super.initView()
        initTopBar()
        mBinding.insert.setOnClickListener {
            val deviceModel = DeviceModel(
                mBinding.contentEt.text.toString(),
                mBinding.typeEt.text.toString(),
                0.5,
                0,
                mBinding.placeEt.text.toString(),
                "故障"
            )
            mViewModel.insertDevice(deviceModel)
        }
        mBinding.timeEt.setOnClickListener{
            mBinding.timeEt.setText(TimeUtils.getCurrentTime())
        }
    }

    override fun initListener() {
        super.initListener()
        mViewModel.addDeviceState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {
                    ToastUtils.showLong(this,"新增失败"+it.message)
                }
                Loading -> {}
                Success -> {
                    ToastUtils.showLong(this,"新增成功")
                    finish()
                }
            }
        }
    }
    fun initTopBar() {
        Glide.with(this).load(com.bawei.lib_common.R.drawable.more).into(mBinding.toolbar.more)
        mBinding.toolbar.title.setText("新增检查工单")
        mBinding.toolbar.back.setOnClickListener {
            finish()
        }
    }


}